package com.weixing.mall.provider.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.enums.ResultCodeEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.api.model.vo.SysUserEditVo;
import com.weixing.mall.provider.api.model.vo.SysUserVo;
import com.weixing.mall.provider.model.domain.User;
import com.weixing.mall.provider.model.dto.sys.ResetPasswordDto;
import com.weixing.mall.provider.model.dto.sys.SysUserChgEnableDto;
import com.weixing.mall.provider.model.dto.sys.SysUserDto;
import com.weixing.mall.provider.model.query.SysUserQuery;
import com.weixing.mall.provider.model.vo.sys.SysUserRoleVo;
import com.weixing.mall.provider.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户接口
 *
 * @author  siashan
 * @since  v1
 */
@Validated
@RestController
@RequestMapping("user")
@Api(value = "系统用户APi", tags = "系统用户")
public class SysUserController extends BaseController {

    @Autowired
    private IUserService sysUserService;


    /**
     * 加载系统用户列表
     * @param sysUserQuery  查询条件
     * @return  用户列表
     */
    @SysLog(type = OperateEnum.R, desp = "查看用户列表")
    @GetMapping("")
    public Result<IPage<SysUserVo>> list(SysUserQuery sysUserQuery) {
        IPage<SysUserVo> list = sysUserService.selectOfPage(initMpPage(), sysUserQuery);
        return ResultUtil.success(list);
    }

    /**
     * 保存用户
     * @param sysUserDto 参数
     * @return 结果
     */
    @PostMapping("save")
    @SysLog(type = OperateEnum.S, desp = "保存用户")
    @NoRepeatSubmit
    public Result save(@Valid SysUserDto sysUserDto) {
        if (null == sysUserDto.getId()) {
            User sysUser = sysUserService.findByLoginName(sysUserDto.getLoginName());
            if (null != sysUser) {
                return ResultUtil.error(ResultCodeEnum.CODE_10012001);
            }
        }
        sysUserService.save(sysUserDto);
        return ResultUtil.success();
    }

    /**
     * 重置密码
     * @param id 用户id
     * @param dto 参数
     * @return 结果
     */
    @PostMapping("resetPwd/{id}")
    public Result resetPwd(@PathVariable long id, ResetPasswordDto dto) {
        User user = sysUserService.getById(id);
        if (null == user) {
            return ResultUtil.error("用户不存在");
        } else {
            sysUserService.changePassword(dto, user);
            return ResultUtil.success();
        }
    }


    /**
     * 启用/禁用
     * @param dto 参数
     * @return 结果
     */
    @ApiOperation(value = "启用/禁用")
    @SysLog(type = OperateEnum.S, desp = "修改用户状态")
    @PostMapping("changeEnable")
    public Result changeEnable(@Validated SysUserChgEnableDto dto) {
        sysUserService.changeEnable(dto.getUserId(), dto.getEnable().getValue());
        return ResultUtil.success();
    }


    /**
     * 删除用户
     * @param id 用户id
     * @return 删除结果
     */
    @SysLog(type = OperateEnum.S, desp = "删除用户")
    @PostMapping("del/{id}")
    public Result del(@PathVariable Long id) {
        User byId = sysUserService.getById(id);
        byId.setIsDel(WhetherEnum.YES.getValue());
        byId.setIsEnable(WhetherEnum.NO.getValue());
        sysUserService.updateById(byId);
        return ResultUtil.success();
    }

    /**
     * 用户详情
     *
     * @param id 用户id
     * @return 用户详情
     */
    @GetMapping("/{id}")
    public Result<SysUserEditVo> user(@PathVariable long id) {
        SysUserEditVo editVo = sysUserService.selectSysUserEditVoById(id);
        return ResultUtil.success(editVo);
    }

    /**
     * 根据角色编码查用户List
     *
     * @apiNote 哪个傻吊写的接口
     * @param roleCode 角色编码
     * @param isEnable 角色是否有效
     * @return 用户列表
     */
    @ApiOperation(value = "根据角色编码查用户List")
    @GetMapping("/getUserByRoleCode")
    public Result< List<SysUserRoleVo>> selectUserByRoleCode(String roleCode,Integer isEnable) {
        List<SysUserRoleVo> sysUserRoleVos = sysUserService.selectSysUserByRoleCode(roleCode,isEnable);
        return ResultUtil.success(sysUserRoleVos);
    }


    /**
     * 重置密码
     *
     * @param dto 参数
     * @return 结果
     */
    @PostMapping("resetPassword")
    public Result resetPwd(ResetPasswordDto dto) {
        SysAuth sysExt = AuthUtil.getSysExt();

        User user = sysUserService.getById(sysExt.getUid());
        if (null == user) {
            return ResultUtil.error("用户不存在");
        } else {
            sysUserService.changePassword(dto, user);
            return ResultUtil.success();
        }
    }


}
